package com.hudongwx.yuan_sdjj.activity;

import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.hudongwx.yuan_sdjj.R;
import com.hudongwx.yuan_sdjj.fragment.LoginFragment;
import com.hudongwx.yuan_sdjj.fragment.RegFragment;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_login)
public class LoginActivity extends AppCompatActivity {
    @ViewInject(R.id.login_view_pager)
    private ViewPager viewPager;
    private Fragment [] pages =new Fragment[2];
    private MyFragmentAdapter myFragmentAdapter;
    @ViewInject(R.id.login_view_tab)
    private TabLayout tabLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(myFragmentAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }
    class MyFragmentAdapter extends FragmentPagerAdapter{

        public MyFragmentAdapter(FragmentManager fm) {
            super(fm);
        }
        //分出两个页面俩写注册和登陆
        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    if(null==pages[position])
                        pages[position]= new LoginFragment();
                    break;
                case 1:
                    if(null==pages[position])
                        pages[position]= new RegFragment();
                    break;
            }
            return pages[position];
        }

        @Override
        public int getCount() {
            return pages.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            if(position==0){
                return "登陆";
            }else {
                return "注册";
            }
        }
    }
    @Event(R.id.login_view_iv_back)
    private void back(View view){
        finish();
    }

}
